<?php
class DeviceTypes {
    #constructor
    public function __construct($dbConnection)
    {
        $this->connection=$dbConnection;
    }
    #end constructor

    private $connection;  
    public $ID;
    public $description;
    
    #methods
    public function getAllDeviceTypes()
    {
        $queries = new mysqlqueries();
        $resultSuppliers = $this->connection->query($queries->GetTable($queries->Devicetype, null));
        $arr = array();
        while ($row = mysqli_fetch_array($resultSuppliers)){
            array_push($arr, $row);
        }        
        return $arr;
    }
    
    public function ExistsOrCreate(){
        $deviceTypes=$this->getAllDeviceTypes();
        $queries = new mysqlqueries();
        $found = false;
        
        foreach ($deviceTypes as $value) {
            if(strtolower($this->description) == strtolower($value[1])){
                $this->ID=$value[0];
                $found = true;
                break;
            }
        }
        
        if($found == false){
            $upArray = array(NULL,$this->description);
            $this->connection->query($queries->Add($queries->Devicetype,$upArray));
            $this->ID = $this->connection->lastID();
        }
        
        return $this->ID;
    }
    #end methods 
}
